package top.truehaku.generator.code.domain.model.code;

import top.truehaku.generator.common.util.PackageUtils;
import top.truehaku.generator.common.util.StringUtils;

public class CodeDirectory {

    /**
     * 项目根路径
     */
    private String projectRootPath;

    /**
     * 子工程路径
     */
    private String subProject;

    /**
     * 项目包
     */
    private String projectPackage;

    /**
     * 模块包名
     */
    private String module;

    /**
     * 相对路径
     */
    private String relativePath;


    public CodeDirectory(String projectRootPath, String subProject, String projectPackage, String module, String relativePath) {
        if(StringUtils.isBlank(projectRootPath)){
            throw new IllegalArgumentException("项目根路径不能为空");
        }
        if(StringUtils.isBlank(projectPackage)){
            throw new IllegalArgumentException("项目包名不能为空");
        }
        this.projectRootPath = StringUtils.trim(projectRootPath);
        this.subProject = StringUtils.trim(subProject);
        this.projectPackage = StringUtils.trim(projectPackage);
        this.module = StringUtils.trim(module);
        this.relativePath = StringUtils.trim(relativePath);
    }

    public String getDirectory(){
        StringBuilder pathBuilder = new StringBuilder(this.projectRootPath);
        if (StringUtils.isNotBlank(this.subProject)) {
            pathBuilder.append("/").append(this.subProject);
        }
        pathBuilder.append("/src/main/java").append("/").append(PackageUtils.toPath(this.projectPackage));
        if (StringUtils.isNotBlank(this.module)){
            pathBuilder.append("/").append(PackageUtils.toPath(this.module));
        }
        pathBuilder.append("/").append(this.relativePath).append("/");
        return pathBuilder.toString().replace("\\","/").replaceAll("//","/");
    }

    public String getPackage(){
        return PackageUtils.toPackage(this.getDirectory());
    }


    public static void main(String[] args) {
        CodeDirectory codeDirectory = new CodeDirectory("D:/workspace/self/spring-boot-examples/generator-spring-boot-starter",
                "",
                "com.iguilty.generator",
                "system",
                "entity"
        );
        System.out.println(codeDirectory.getDirectory());
        System.out.println(codeDirectory.getPackage());


    }

}
